﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FireBall : MonoBehaviour {

	// Use this for initialization
    public Vector3 m_vAction { get; set; }
    public bool m_bIsActive{ get; set; }
    public float m_fVel { get; set; }
    public Transform m_tPlayer { get; set; }
    private float m_fLife;
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (m_bIsActive && GameData.gi.bIsGameRunning)
        {
            transform.Translate(GameData.gi.fGameSpeed * GameData.gi.SGR.fGameSpeed * 5 * Time.deltaTime * m_vAction);
            m_fLife -= GameData.gi.fGameSpeed * GameData.gi.SGR.fGameSpeed * Time.deltaTime;
            if (m_fLife <= 0)
                Destroy(gameObject);
        }
	}

    public void InitialFireBall(int iType,float fLife)
    {
        if (iType == 1)
        {
            gameObject.name = "FireBall";
            m_vAction = new Vector3(0, -GameData.fbi.fFallBallV, 0);
            gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture(GameData.gii.sFallBallImage));
            CommonFunctionA.PlayAudio(gameObject, GameData.spi.gmi.sFallBallSound);            
        }
        else if (iType == 2)
        {
            gameObject.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture(GameData.gii.sFallBallImage));
            gameObject.transform.GetChild(0).name = "FireBall_Follow";
            Vector3 rotation = transform.localEulerAngles;
            rotation.z = UnityEngine.Random.Range(-GameData.fbi.fFallBall2Range, GameData.fbi.fFallBall2Range); 
            transform.localEulerAngles = rotation;
            m_vAction = new Vector3(0, -GameData.fbi.fFallBall2V, 0);
            CommonFunctionA.PlayAudio(gameObject, GameData.spi.gmi.sFallBallSound);
        }
        else if (iType == 3)
        {
            //CommonFunctionA.PlayAudio(gameObject,
            gameObject.GetComponent<SpriteRenderer>().sprite = CommonFunctionA.SetSpriteTexture(CommonFunctionA.loadPicture(GameData.gii.sFlyBirdImage));
            gameObject.name = "Bird";
            //Vector3 rotation = transform.localEulerAngles;
            //rotation.z = UnityEngine.Random.Range(-GameData.fbi.fFlyBirdRange, GameData.fbi.fFlyBirdRange);
            //transform.localEulerAngles = rotation;
            
            //根据终点与起点的长度，计算角度
            Vector3 v = new Vector3(m_tPlayer.position.x + Random.Range(-3f, 3f), m_tPlayer.position.y + Random.Range(-5f, 5f), m_tPlayer.position.z);
            float fX = Mathf.Abs(v.x - transform.position.x);
            float fY = Mathf.Abs(v.y - transform.position.y);
            float fA = Mathf.Atan(fY / fX)/Mathf.PI*180;
            //CommonFunctionA.logger("fx:" + fX + ",fy:" + fY + "fA:" + fA, 1);
            if (m_tPlayer.position.y >= transform.position.y)
            {
                fA = fA;
            }
            else
            {
                fA = -fA;
            }
            Vector3 rotation = transform.localEulerAngles;
            rotation.z = fA;
            transform.localEulerAngles = rotation;
            m_vAction = new Vector3(GameData.fbi.fFlyBirdV,0, 0);
            CommonFunctionA.PlayAudio(gameObject, GameData.spi.gmi.sFlyBirdSound);
        }
        m_bIsActive = true;
        m_fLife = fLife;
    }
}
